import { ecs } from "db://oops-framework/libs/ecs/ECS";
import { Scene } from "../Scene";
import { BattleResultType } from "../model/BattleEnum";
import { Role } from "../../../role/Role";
import { RoleState } from "../../skill/bll/utils/RoleState";
import { Skill } from "../../skill/Skill";
import { oops } from "db://oops-framework/core/Oops";
import { BattleEventTrigger } from "../../skill/bll/utils/BattleEventTrigger";
import { RoleAttributeType } from "../../../role/model/RoleEnum";

/** 
 * 自动战斗
 * 1、AI制动选择角色一个个出发技能
 * 2、每播放完一个角色的技能流程，出发技能系统
 */
@ecs.register('BattleAuto')
export class BattleAutoComp extends ecs.Comp {
    reset() {

    }
}

export class BattleAutoSystem extends ecs.ComblockSystem implements ecs.IEntityEnterSystem {
    filter(): ecs.IMatcher {
        return ecs.allOf(BattleAutoComp);
    }

    entityEnter(e: Scene): void {
        this.onRoleActiveComplete(e);
    }
    private onRoleActiveComplete(e: Scene) {
        if (e.BattleModel.battleResultType != BattleResultType.Battling) {
            return
        }
        let caster: Role = this.getCanActiveRole(e.MapModelRole.roles);
        // 当前角色攻击
        if (caster) {
            let skill!: Skill;
            // 主动技能，在Power当前值到100时，自动施放主动技能，否则施放普通攻击
            if (caster.RoleModel.table.hasPower && caster.getNumericValue(RoleAttributeType.power) >= caster.getNumericValue(RoleAttributeType.power_max)) {
                skill = caster.RoleModelSkill.power;
            }
            else {
                skill = caster.RoleModelSkill.normal;
            }
            if (!skill) {
                console.error(`【战斗】【${caster.RoleModel.pos}号位：${caster.RoleModel.table.name}】没有技能可释放`);
                e.roundEnd();
                e.remove(BattleAutoComp);
            }
            if (caster && skill.SkillModel.table.skill_type != 3) {
                caster.RoleModelSkill.currentSkill = skill;
                oops.log.trace(`【战斗】【${caster.RoleModel.pos}号位：${caster.RoleModel.table.name}】开始施放技能`);
            }


            // 技能事件：角色行动前
            BattleEventTrigger.onActionBefore(caster);

            skill.cast(caster);

            //友军释放技能时
            BattleEventTrigger.onFriendSkillCasting(caster, skill);

            // 技能事件：角色行动后
            BattleEventTrigger.onActionAfter(caster);
            this.action_over(e, caster);

            // 技能动画
            e.play(() => {
                setTimeout(() => {
                    this.onRoleActiveComplete(e);
                }, 200 / oops.game.getTimeScale());
            });

            if (caster && skill.SkillModel.table.skill_type != 3) {
                e.MapModelRole.current_skill = null!;
                oops.log.trace(`【战斗】【${caster.RoleModel.pos}号位：${caster.RoleModel.table.name}】结束施放技能`);
            }
        }
        // 下一回合
        else {
            e.roundEnd();
            e.remove(BattleAutoComp);
        }
    }

    /** 获取能行动的角色 */
    private getCanActiveRole(roles: Role[]): Role {
        let r: Role = null!;
        for (let i = 0; i < roles.length; i++) {
            let role = roles[i];
            let rm = role.RoleModel;
            // 注：不可行动可能因技能BUFF效果导致当前回合无法移动或控制角色操作
            if (RoleState.isDie(role)) {
                continue;
            }
            if (RoleState.isDizzy(role)) {
                continue;
            }
            if (RoleState.isFrozen(role)) {
                continue;
            }
            //宝箱不攻击
            if (role.RoleModel.table.type == 5) {
                continue;
            }
            // 找到可行动的角色
            if (!rm.active) {
                r = role;
                break;
            }
        }
        return r;
    }
    /** 角色行动结束 */
    private action_over(e: Scene, role: Role) {
        // 角色当前回合结束标记
        role.RoleModel.active = true;
    }
}